<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">信息管理</span>
    <el-button type="primary" style="float:right;margin-top:13px;"
               @click="xinjianhuodong=true">+ 新建活动</el-button>
  </div>
  <div class="activity-container">
    <el-card class="filter-card">
      <el-form :model="queryParams" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="活动标题">
              <el-input
                  v-model="queryParams.title"
                  placeholder="请输入活动标题"
                  clearable
              />
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="活动类型">
              <el-select
                  v-model="queryParams.type"
                  placeholder="类型选择"
                  clearable
              >
                <el-option
                    v-for="item in activityTypes"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="活动渠道">
              <el-select
                  v-model="queryParams.channel"
                  placeholder="渠道选择"
                  clearable
              >
                <el-option
                    v-for="item in channels"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="活动状态">
              <el-select
                  v-model="queryParams.status"
                  placeholder="请选择活动状态"
                  clearable
              >
                <el-option
                    v-for="item in statusOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="营销方式">
              <el-select
                  v-model="queryParams.marketingType"
                  placeholder="请选择营销方式"
                  clearable
              >
                <el-option
                    v-for="item in marketingTypes"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="活动时间">
              <el-date-picker
                  v-model="queryParams.dateRange"
                  type="daterange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  value-format="YYYY-MM-DD"
              />
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="区域选择">
              <el-select
                  v-model="queryParams.region"
                  placeholder="请选择营销的备件/区县"
                  clearable
              >
                <el-option
                    v-for="item in regions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="24" style="text-align: right;">
            <el-button @click="handleReset">重置</el-button>
            <el-button type="primary" @click="handleQuery">查询</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <el-card class="table-card">
      <div class="table-title">活动列表</div>
      <el-table
          :data="tableData"
          style="width: 100%"
          v-loading="loading"
          empty-text="暂无数据"
      >
        <el-table-column prop="id" label="编号" width="80" />
        <el-table-column prop="title" label="活动标题" min-width="120" />
        <el-table-column prop="target" label="活动目标" min-width="120" />
        <el-table-column prop="type" label="活动类型" width="100" />
        <el-table-column prop="reward" label="活动奖励" width="100" />
        <el-table-column prop="shop" label="店铺" width="100" />
        <el-table-column prop="channel" label="渠道" width="100" />
        <el-table-column prop="marketingType" label="营销方式" width="120" />
        <el-table-column prop="time" label="活动时间" width="180" />
        <el-table-column prop="status" label="活动状态" width="100" />
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
            v-model:current-page="queryParams.pageNum"
            v-model:page-size="queryParams.pageSize"
            :total="total"
            :page-sizes="[10, 20, 30, 50]"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
  <!-- 新建活动弹窗 -->
  <el-dialog title="新建活动" v-model="xinjianhuodong" style="margin-top: 37vh;">
    <div style="text-align: center; width: 100%;">
     <el-row :gutter="12">
       <el-col :span="4">
        <el-button style="width: 100px;height: 100px ">赠领</el-button>
       </el-col>
       <el-col :span="4">
         <el-button style="width: 100px;height: 100px ">拼团</el-button>
       </el-col>
       <el-col :span="4">
         <el-button style="width: 100px;height: 100px ">秒杀</el-button>
       </el-col>
       <el-col :span="4">
         <el-button style="width: 100px;height: 100px ">返现</el-button>
       </el-col>
       <el-col :span="4">
         <el-button style="width: 100px;height: 100px ">抽奖</el-button>
       </el-col>
     </el-row>
    </div>
    <template #footer>
      <div style="text-align: center; width: 100%;">
        <el-button type="primary"> + 新建活动</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'

//新建活动弹窗
const xinjianhuodong = ref(false);
// 查询参数
const queryParams = reactive({
  title: '',
  type: '',
  channel: '',
  status: '',
  marketingType: '',
  dateRange: [],
  region: '',
  pageNum: 1,
  pageSize: 10
})

// 表格数据
const tableData = ref([])
const total = ref(0)
const loading = ref(false)

// 下拉选项数据
const activityTypes = ref([
  { value: '1', label: '促销活动' },
  { value: '2', label: '会员活动' },
  { value: '3', label: '节日活动' }
])

const channels = ref([
  { value: '1', label: '线上' },
  { value: '2', label: '线下' },
  { value: '3', label: '全渠道' }
])

const statusOptions = ref([
  { value: '1', label: '未开始' },
  { value: '2', label: '进行中' },
  { value: '3', label: '已结束' },
  { value: '4', label: '已取消' }
])

const marketingTypes = ref([
  { value: '1', label: '折扣' },
  { value: '2', label: '满减' },
  { value: '3', label: '赠品' },
  { value: '4', label: '积分' }
])

const regions = ref([
  { value: '1', label: '北京' },
  { value: '2', label: '上海' },
  { value: '3', label: '广州' },
  { value: '4', label: '深圳' }
])

// 查询活动列表
const getList = () => {
  loading.value = true
  // 这里应该是API调用，模拟数据
  setTimeout(() => {
    tableData.value = [
      {
        id: '1',
        title: '五一促销活动',
        target: '提升销售额',
        type: '促销活动',
        reward: '满100减20',
        shop: '旗舰店',
        channel: '线上',
        marketingType: '满减',
        time: '2023-04-28 至 2023-05-07',
        status: '已结束'
      },
      {
        id: '2',
        title: '夏季会员日',
        target: '增加会员数',
        type: '会员活动',
        reward: '双倍积分',
        shop: '所有门店',
        channel: '全渠道',
        marketingType: '积分',
        time: '2023-06-15 至 2023-06-18',
        status: '进行中'
      }
    ]
    total.value = 2
    loading.value = false
  }, 500)
}

// 查询按钮
const handleQuery = () => {
  queryParams.pageNum = 1
  getList()
}

// 重置按钮
const handleReset = () => {
  Object.assign(queryParams, {
    title: '',
    type: '',
    channel: '',
    status: '',
    marketingType: '',
    dateRange: [],
    region: '',
    pageNum: 1,
    pageSize: 10
  })
  getList()
}

// 分页大小变化
const handleSizeChange = (val) => {
  queryParams.pageSize = val
  getList()
}

// 当前页变化
const handleCurrentChange = (val) => {
  queryParams.pageNum = val
  getList()
}

// 编辑操作
const handleEdit = (row) => {
  ElMessage.info(`编辑活动：${row.title}`)
  // 这里应该是打开编辑对话框的逻辑
}

// 初始化加载数据
onMounted(() => {
  getList()
})
</script>

<style scoped>
.activity-container {
  padding: 20px;
}

.filter-card {
  margin-bottom: 20px;
}

.table-card {
  margin-bottom: 20px;
}

.table-title {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 15px;
}

.pagination-container {
  margin-top: 15px;
  display: flex;
  justify-content: flex-end;
}
</style>>